package com.daka.tools.rongYun;

import com.alibaba.fastjson.JSON;
import com.daka.tools.rongYun.models.*;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 使用融云  向APP端推送信息
 * Created by chunyang on 2016/6/2.
 */
@Component
public class PushMessageUtil
{
    //Test
    //public static String KEY = "bmdehs6pdo82s";
    //public static String SECRET = "odhxkmxFIauAv";

    //Produect
    public static String KEY = "pkfcgjstf6ie8";
    public static String SECRET = "LVDa81etX0qD";

    public static String title = "志愿者打卡器";

    /**
     * 发送单聊消息
     * @param content
     * @param params
     * @param fromUser
     * @param toUsers
     * @return
     */
    public static SdkHttpResult pushMessage(String content, Map<String, String> params,String fromUser,List<String> toUsers)
    {
//        SdkHttpResult result = null;
//        try
//        {
//            PushMessage message = createPushMessage(title,content,params,fromUser,toUsers);//融云消息
//            result = ApiHttpClient.push(KEY, SECRET, message, FormatType.json);
//        } catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//        return result;

        SdkHttpResult result = null;
        try
        {
            System.out.println(content+";"+params+";"+fromUser+";"+toUsers);
            result = ApiHttpClient.publishSystemMessage(KEY,SECRET,fromUser,toUsers,new TxtMessage(content, JSON.toJSONString(params)),content,JSON.toJSONString(params),FormatType.json);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 系统消息
     * @param content
     * @param params
     * @param fromUser
     * @param toUsers
     * @return
     */
    public static SdkHttpResult publishSystemMessage(String content, Map<String, String> params,String fromUser,List<String> toUsers)
    {
        SdkHttpResult result = null;
        try
        {
            System.out.println(content+";"+params+";"+fromUser+";"+toUsers);
            result = ApiHttpClient.publishSystemMessage(KEY,SECRET,fromUser,toUsers,new TxtMessage(content, JSON.toJSONString(params)),content,JSON.toJSONString(params),FormatType.json);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 创建群组
     * @param userIds
     * @param groupId
     * @param groupName
     * @return
     */
    public static SdkHttpResult createGroup(List<String> userIds,String groupId,String groupName)
    {
        SdkHttpResult result = null;
        try
        {
            result = ApiHttpClient.createGroup(KEY,SECRET,userIds,groupId,groupName, FormatType.json);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 添加禁言群成员 方法
     * @param userIds
     * @param groupId
     * @param minute
     * @return
     */
    public static SdkHttpResult gagAdd(List<String> userIds,String groupId,int minute)
    {
        SdkHttpResult result = null;
        try
        {
            result = ApiHttpClient.gagAdd(KEY, SECRET, userIds, groupId, minute, FormatType.json);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 移除禁言群成员 方法
     * @param userIds
     * @param groupId
     * @return
     */
    public static SdkHttpResult gagRollback(List<String> userIds,String groupId)
    {
        SdkHttpResult result = null;
        try
        {
            result = ApiHttpClient.gagRollback(KEY, SECRET, userIds, groupId, FormatType.json);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }
    public static SdkHttpResult publishGroupMessage(String fromUserId,List<String> toGroupIds,String pushContent,String groupName)
    {
        SdkHttpResult result = null;
        try
        {
            Message msg = new InfoMessage(pushContent,groupName);
            result = ApiHttpClient.publishGroupMessage(KEY, SECRET,fromUserId, toGroupIds, msg, pushContent, pushContent, FormatType.json);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;


    }

    /**
     * 加入群组
     * @param userId
     * @param groupId
     * @param groupName
     * @return
     */
    public static SdkHttpResult joinGroup(String userId,String groupId,String groupName)
    {
        SdkHttpResult result = null;
        try
        {
            result = ApiHttpClient.joinGroup(KEY, SECRET, userId, groupId, groupName, FormatType.json);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }
    public static SdkHttpResult exitGroup(String userId, String groupCode)
    {
        SdkHttpResult result = null;
        try
        {
            result = ApiHttpClient.quitGroup(KEY, SECRET, userId, groupCode, FormatType.json);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 创建发送落地消息的内容
     */
    public static PushMessage createPushMessage(String title, String content, Map<String, String> params,String fromUser,List<String> users)
            throws UnsupportedEncodingException
    {
        List<String> osList = new ArrayList<String>();
        osList.add("ios");
        osList.add("android");

        TagObj tag = new TagObj();
        tag.setIs_to_all(false);//给全量用户发 设置为true则下面标签和userids将无效

        //给特定用户ID发,优先级高于上面的标签
        tag.setUserid(users);

        PushMessage pmsg = new PushMessage();
        pmsg.setPlatform(osList);
        PushNotification noti = new PushNotification();
        noti.setAlert(title);
        PlatformNotification platformNotification = createPush(title,params);
        noti.setAndroid(platformNotification);
        noti.setIos(platformNotification);
        pmsg.setNotification(noti);

        //下面两个参数的有无 控制发送的落地消息还是不落地的push
        //PushMessage实体中的 fromuserid 和 message为null即为不落地的push
        pmsg.setFromuserid(fromUser);
        MsgObj msg = new MsgObj();
        String param = null;
        if(null != params)
            param = JSON.toJSONString(param);

        TxtMessage message = new TxtMessage(content,param);
        msg.setContent(message.toString());
        msg.setObjectName(message.getType());
        pmsg.setMessage(msg);
        //上面两个参数的有无 控制发送的落地消息还是不落地的push

        pmsg.setAudience(tag);
        System.out.println(pmsg.toString());
        return pmsg;
    }

    private static PlatformNotification createPush(String title,Map<String, String> map)
    {
        PlatformNotification data = new PlatformNotification();
        data.setAlert(title);
        if(null != map)
            data.setExtras(map);
        return data;
    }



}
